package com.huibei.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huibei.pojo.House;
import com.huibei.pojo.Order;
import com.huibei.service.IHouseService;
import com.huibei.service.IOrderService;
import com.huibei.util.ReturnData;
import com.huibei.util.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IHouseService houseService;

    @RequestMapping("/toOrder")
    public Object toOrder(@RequestParam Map<String, Object> map) throws ParseException {
        System.out.println("前端传递的信息为：" + map);
        System.out.println("前端传递的信息为：" + map);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
       // SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Order order = new Order(map.get("orderId") + "",Integer.parseInt(map.get("userId") + ""),null,null,Integer.parseInt(map.get("houseId") + ""),
                format.parse(map.get("comeDate") + "") , format.parse(map.get("leaveDate") + "") ,Integer.parseInt(map.get("totalPrice") + ""),Integer.parseInt(map.get("liveDays") + ""), 0);
        if(orderService.save(order)){
            //System.out.println("插入成功");
            return new ReturnData(StatusCode.REQUEST_SUCCESS, true, "数据插入成功");
        }else{
            return new ReturnData(StatusCode.DATA_NULL,false,"查询数据失败");
        }
    }

    @RequestMapping("/detail")
    public Object orderDetail(@RequestParam Map<String,Object> map){
        System.out.println(map);

        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("ORDER_ID", map.get("orderId"));
        Map orderById= orderService.getMap(wrapper);
        System.out.println(orderById);
       // Map<String, Object> roomById = (Map<String, Object>) houseService.getById(orderById.get("HOUSE_ID") + "");
        QueryWrapper<House> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("HOUSE_ID", orderById.get("HOUSE_ID"));
        Map roomById =  houseService.getMap(wrapper1);
        orderById.put("HOUSE_NAME",roomById.get("HOUSE_NAME"));
        orderById.put("HOUSE_CITY",roomById.get("HOUSE_CITY"));
        /*
        Map<String, Object> orderById = service.getOrderById(map);
        System.out.println("结果："+orderById);
        Map<String,Object> map1 = new HashMap<>();
        Object house_id = orderById.get("HOUSE_ID");
        map1.put("houseId",house_id);
        Map<String, Object> roomById = cityService.getRoomById(map1);
        orderById.put("HOUSE_NAME",roomById.get("HOUSE_NAME"));
        orderById.put("HOUSE_CITY",roomById.get("HOUSE_CITY"));
        */
        return new ReturnData(StatusCode.REQUEST_SUCCESS,orderById,"成功");
    }

    //根据userId拿到用户的所有订单
    @RequestMapping(value = "/getOrderList")
    public Object getOrderList(@RequestParam Map<String,Object> map){
        System.out.println("=============>" + map);
        System.out.println("=============>" + map);

        List<Map<String, Object>> orderList = (List<Map<String, Object>>) orderService.getOrderList(map);
        System.out.println(orderList);

        if (orderList.size()>0){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,orderList,"交互成功");
        }else {
            return new ReturnData(StatusCode.DATA_NULL,-1,"查询错误或者不存在订单");
        }
    }

    //修改入住人员信息
    @RequestMapping("/changeInfo")
    public Object changeInfo(@RequestParam Map<String,Object> map){
        System.out.println("修改信息:"+map);
        int i = orderService.changeInfo(map);
        if (i == 1){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,1, "成功");
        }else {
            return new ReturnData(StatusCode.RETURN_ERROR,0,"失败");
        }
    }

    //修改订单状态码
    @RequestMapping("/changeStatusWhat")
    public Object changeStatus(@RequestParam Map<String,Object> map){
        System.out.println("支付订单："+map);
        int i = orderService.changeStatus(map);
        if (i == 1){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,1,"成功");
        }else{
            return new ReturnData(StatusCode.RETURN_ERROR,0,"失败");
        }
    }

    @RequestMapping("/cancelOrder")
    public Object cancelOrder(@RequestParam Map<String,String> map){
        System.out.println("删除订单：" + map.get("orderId"));
        int i = orderService.cancelOrder(map);
        System.out.println(i);
        if (i == 1){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,true,"取消订单成功");
        }else {
            return new ReturnData(StatusCode.RETURN_ERROR,false,"取消失败");
        }
    }

}
